package com.xiaoshuidi.cloud.module.contract.controller.admin.contract;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.customerCompany.*;
import com.xiaoshuidi.cloud.module.contract.convert.contract.CustomerCompanyConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.CustomerCompanyDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.contract.CustomerCompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 企业客户")
@RestController
@RequestMapping(ApiConstants.NAME + "/customer-company")
@Validated
public class CustomerCompanyController {

    @Resource
    private CustomerCompanyService customerCompanyService;

    @PostMapping("/create")
    @Operation(summary = "创建企业客户")
//    @PreAuthorize("@ss.hasPermission('contract:customer:create')")
    public CommonResult<Long> createCustomerCompany(@Valid @RequestBody CustomerCompanyCreateReqVO createReqVO) {
        return success(customerCompanyService.createCustomerCompany(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业客户")
//    @PreAuthorize("@ss.hasPermission('contract:customer:update')")
    public CommonResult<Boolean> updateCustomerCompany(@Valid @RequestBody CustomerCompanyUpdateReqVO updateReqVO) {
        customerCompanyService.updateCustomerCompany(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业客户")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('contract:customer:delete')")
    public CommonResult<Boolean> deleteCustomerCompany(@RequestParam("id") Long id) {
        customerCompanyService.deleteCustomerCompany(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业客户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('contract:customer:query')")
    public CommonResult<CustomerCompanyRespVO> getCustomerCompany(@RequestParam("id") Long id) {
        CustomerCompanyRespVO respVO = customerCompanyService.getCustomerCompany(id);
        return success(respVO);
    }


    @GetMapping("/page")
    @Operation(summary = "获得企业客户分页")
//    @PreAuthorize("@ss.hasPermission('contract:customer:query')")
    public CommonResult<PageResult<CustomerCompanyRespVO>> getCustomerCompanyPage(@Valid CustomerCompanyPageReqVO pageVO) {
        PageResult<CustomerCompanyDO> pageResult = customerCompanyService.getCustomerCompanyPage(pageVO);
        return success(CustomerCompanyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/page-renting-room")
    @Operation(summary = "获得企业房间分页")
//    @PreAuthorize("@ss.hasPermission('contract:customer:query')")
    public CommonResult<PageResult<CompanyRentRoomRespVO>> getCompanyRentRoomPage(@Valid CompanyRentRoomPageReqVO pageVO) {
        return success(customerCompanyService.getCompanyRentRoomPage(pageVO));
    }

    @PutMapping("/change-hydropower-tenantry")
    @Operation(summary = "变更水电承租方")
//    @PreAuthorize("@ss.hasPermission('contract:customer:update')")
    public CommonResult<Boolean> changeHydropowerTenantry(@Valid @RequestBody CompanyChangeUndertakerReqVO reqVO) {
        customerCompanyService.changeHydropowerTenantry(reqVO);
        return success(true);
    }

    /**
     * 禁用企业客户
     */
    @PutMapping("/disabled")
    @Operation(summary = "禁用企业客户")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('contract:customer:delete')")
    public CommonResult<Boolean> disabledCustomerCompany(@RequestParam("id") Long id) {
        customerCompanyService.disabledCustomerCompany(id);
        return success(true);
    }
}
